﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JieBooLaw.Common.Util
{
    public class ConvertUtil
    {
        public static int ToInt(object source)
        {
            if (source == null)
            {
                return 0;
            }
            int result;

            if (int.TryParse(source.ToString(), out result))
                return result;

            return 0;
        }

        public static long ToLong(object source)
        {
            if (source == null)
            {
                return 0;
            }
            long result;

            if (long.TryParse(source.ToString(), out result))
                return result;

            return 0;
        }

        public static Decimal ToDecimal(object source)
        {
            if (source == null)
            {
                return 0;
            }
            Decimal result;

            if (Decimal.TryParse(source.ToString(), out result))
                return result;

            return 0;
        }

        public static DateTime ToDateTime(object source)
        {
            if (source == null)
            {
                return DateTime.MinValue;
            }
            DateTime result;

            if (DateTime.TryParse(source.ToString(), out result))
                return result;

            return result;
        }

        public static double ToDouble(object source)
        {
            if (source == null)
            {
                return 0.0;
            }
            double result = 0.0;

            if (double.TryParse(source.ToString(), out result))
                return result;

            return 0.0;
        }

        public static bool ToBool(object source)
        {
            if (source == null)
                return false;
            bool val = false;
            bool.TryParse(source.ToString(), out val);

            return val;
        }
    }
}
